﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using OgilvyOne.Ext.Weibo.Model;
using OgilvyOne.Ext.Weibo.Common;
using OgilvyOne.Sharan.BLL;

namespace OgilvyOne.Sharan.ProcessFlow
{
    [Serializable]
    public class AccountController
    {
        public const string SESSION_KEY_USERID = "SESSION_KEY_USERID";
        public const string SESSION_KEY_SEARCH = "SESSION_KEY_SEARCH";
        public const string SESSION_KEY_ROWINDEX = "SESSION_KEY_ROWINDEX";
        public const string SESSION_KEY_PAGEINDEX = "SESSION_KEY_PAGEINDEX";
        public const string SESSION_KEY_USERS = "SESSION_KEY_USERS";
        public const string SESSION_KEY_RANDOM = "SESSION_KEY_RANDOM";
        public const string SESSION_KEY_ACCESS_TOKEN = "SESSION_KEY_ACCESS_TOKEN";

        public long? WeiboUserID
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_USERID);
                if (o == null) return null;
                return long.Parse(o.ToString());
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_USERID] = value;
            }
        }

        public string Search
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_SEARCH);
                if (o == null) return null;
                return o.ToString();
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_SEARCH] = value;
            }
        }

        public int RowIndex
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_ROWINDEX);
                if (o == null) return -1;
                return int.Parse(o.ToString());
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_ROWINDEX] = value;
            }
        }

        public int PageIndex
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_PAGEINDEX);
                if (o == null) return 1;
                return int.Parse(o.ToString());
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_PAGEINDEX] = value;
            }
        }

        public int Random
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_RANDOM);
                if (o == null)
                {
                    Random r = new Random();
                    int templateID = r.Next() % ConfigCacheManager.TemplateCount;
                    templateID++;
                    HttpContext.Current.Session[SESSION_KEY_RANDOM] = templateID;
                    return templateID;
                }
                return int.Parse(o.ToString());
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_RANDOM] = value;
            }
        }

        public Users Users
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_USERS);
                if (o == null) return null;
                return (Users)o;
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_USERS] = value;
            }
        }

        public OAuthAccessToken AccessToken
        {
            get
            {
                object o = RetrieveObjectFromSession(SESSION_KEY_ACCESS_TOKEN);
                if (o == null) return null;
                return (OAuthAccessToken)o;
            }
            set
            {
                HttpContext.Current.Session[SESSION_KEY_ACCESS_TOKEN] = value;
            }
        }

        private object RetrieveObjectFromSession(string key)
        {
            if (HttpContext.Current.Session != null)
            {
                object o = HttpContext.Current.Session[key];
                return o;
            }
            return null;
        }

        public AccountController()
        {
        }

        
    }
}